package com.ziyun.erp.modules.otherAccessory.service.impl;

import com.ziyun.erp.common.e.DeleteEnum;
import io.renren.common.utils.R;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.otherAccessory.dao.OtherAccessoryDao;
import com.ziyun.erp.modules.otherAccessory.entity.OtherAccessoryEntity;
import com.ziyun.erp.modules.otherAccessory.service.OtherAccessoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("otherAccessoryService")
public class OtherAccessoryServiceImpl extends ServiceImpl<OtherAccessoryDao, OtherAccessoryEntity> implements OtherAccessoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<OtherAccessoryEntity> page = this.selectPage(
                new Query<OtherAccessoryEntity>(params).getPage(),
                new EntityWrapper<OtherAccessoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean updateByMap(Map<String, Object> updateMap, String Id) {
        int count = this.baseMapper.updateByMap(updateMap, Id);
        if (count == 0){
            return false;
        }
        return true;
    }

    @Override
    public R queryImageList(Map<String, Object> params) {
        String imageUrl = (String) params.get("imageUrl");
        List<OtherAccessoryEntity> otherAccessoryEntityList = this.baseMapper.selectList(new EntityWrapper<>(new OtherAccessoryEntity())
                .in("id", Arrays.asList(imageUrl.split(",")))
                .eq("disabled", DeleteEnum.NOT_DELETE.toInt())
        );
        return R.ok().put("list", otherAccessoryEntityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOtherAccessory(List<Integer> ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("disabled", DeleteEnum.DELETE.toInt());
        for (Integer id : ids){
            this.baseMapper.updateByMap(param, id.toString());
        }
    }

}
